/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.appspot.ittrick.drive.trick;

import com.appspot.ittrick.drive.trick.controller.MainController;
import com.appspot.ittrick.drive.trick.controller.MenuController;
import com.appspot.ittrick.drive.trick.controller.OAuthController;
import com.appspot.ittrick.drive.trick.controller.SystemErrorPopWindowController;
import java.io.IOException;
import javafx.application.Application;
import javafx.application.HostServices;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.layout.Pane;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.StageBuilder;

/**
 *
 * @author takahara
 */
public class DriveTrick extends Application {
    
    /**
     * DriveTrick instance
     */
    private static DriveTrick instance;
    /**
     * ステージ
     */
    private Stage stage;

    /**
     * 承認ステージ
     */
    private Stage oauthStage;
    /**
     * ファイルステージ
     */
    private Stage fileStage;
    /**
     * popStage
     */
    private Stage popStage;
    
    private HostServices hostServices;
    
    
    
    /**
     * コンストラクタ
     */
    public DriveTrick() {
        this.setInstance();
    }
    
    /**
     * startメソッド
     * @param primaryStage
     * @throws Exception 
     */
    @Override
    public void start(Stage primaryStage) throws Exception {
//        System.setProperty("file.encoding", "Shift_JIS");
        
        stage = primaryStage;
        stage.setWidth(800);
        stage.setHeight(600);
        stage.getIcons().add(new Image(DriveTrick.class.getResourceAsStream("images/drive-trick-logo.png")));
        
        sendMenuController();
        stage.show();
        
        hostServices = getHostServices();
    }

    /**
     * mainメソッド
     * @param args 
     */
    public static void main(String[] args) {
        launch(args);
    }
    
    /**
     * インスタンスの設定
     */
    private void setInstance() {
        instance = this;
    }
    
    /**
     * メニュー画面へ遷移する
     */
    public void sendMenuController() {

        stage.setTitle(Constants.APPLICATION_NAME);

        MenuController controller = new MenuController();
        this.replaceSceneContent(controller);
    }
    
    /**
     * メイン画面へ遷移する
     * @param mailAddress
     */
    public void sendMainController(String mailAddress) {

        stage.setTitle(Constants.APPLICATION_NAME);

        MainController controller = new MainController(mailAddress);
        this.replaceSceneContent(controller);
    }
    
    /**
     * OAuth承認画面を表示
     * @param controller
     * @throws java.io.IOException
     */
    public void showOAuthStage(OAuthController controller) throws IOException {
        
        // 承認用ステージの初期化
        if (oauthStage == null) {
            oauthStage = new Stage();

            oauthStage.initOwner(this.getStage());
            oauthStage.initModality(Modality.APPLICATION_MODAL);
            oauthStage.setTitle("アカウント承認");
            oauthStage.getIcons().add(new Image(DriveTrick.class.getResourceAsStream("images/drive-trick-logo.png")));
            oauthStage.setWidth(500);
            oauthStage.setHeight(600);
        }
        
        
        Scene scene = oauthStage.getScene();
        if (scene == null) {
            scene = new Scene(controller);
            oauthStage.setScene(scene);
        } else {
            oauthStage.getScene().setRoot(controller);
        }

        oauthStage.show();
    }
    
    /**
     * ファイルステージを表示
     * @param controller
     */
    public void showFileStage(Pane controller) {
        // 承認用ステージの初期化
        if (fileStage == null) {
            fileStage = new Stage();

            fileStage.initOwner(this.getStage());
            fileStage.initModality(Modality.APPLICATION_MODAL);
            fileStage.setTitle("file view");
            fileStage.setWidth(800);
            fileStage.setHeight(600);
        }
        
        
        Scene scene = fileStage.getScene();
        if (scene == null) {
            scene = new Scene(controller);
            fileStage.setScene(scene);
        } else {
            fileStage.getScene().setRoot(controller);
        }

        fileStage.show();
    }
    
    /**
     * ポップステージを表示
     * @param controller
     * @param title
     * @param width
     * @param height
     */
    public void showPopStage(Pane controller, String title, double width, double height) {
        
        if (popStage == null) {
            popStage = new Stage();

            popStage.initOwner(this.getStage());
            popStage.initModality(Modality.APPLICATION_MODAL);
            popStage.setTitle(title);
            popStage.getIcons().add(new Image(DriveTrick.class.getResourceAsStream("images/drive-trick-logo.png")));
            popStage.setWidth(width);
            popStage.setHeight(height);
        }
        
        
        Scene scene = popStage.getScene();
        if (scene == null) {
            scene = new Scene(controller);
            popStage.setScene(scene);
        } else {
            popStage.getScene().setRoot(controller);
        }

        popStage.show();
    }
    
    /**
     * OAuth承認画面を閉じる
     */
    public void closeOAuthStage() {
        oauthStage.close();
    }
    
    /**
     * ポップ承認画面を閉じる
     */
    public void closePopStage() {
        popStage.close();
    }
    
    
    /**
     * メインステージの取得
     *
     * @return
     */
    public Stage getStage() {
        return stage;
    }
    
    /**
     * Get App Instance
     *
     * @return
     */
    public static DriveTrick getInstance() {
        return instance;
    }
    
    
    /**
     * ページ遷移
     *
     * @param controller
     */
    private void replaceSceneContent(Parent controller) {
        Scene scene = stage.getScene();
        if (scene == null) {
            scene = new Scene(controller);
            stage.setScene(scene);
        } else {
            stage.getScene().setRoot(controller);
        }
    }
    
    /**
     * エラーポップアップ画面を表示
     * @param exception
     */
    public void displayErrorPop(Exception exception) {

        Parent controller = new SystemErrorPopWindowController(exception);

        Scene scene = new Scene(controller);
        Stage errorPopStage = StageBuilder.create()
                .scene(scene)
                .title("システムエラー")
                .width(600)
                .height(400)
                .build();

        errorPopStage.initOwner(stage);
        errorPopStage.initModality(Modality.APPLICATION_MODAL);
        errorPopStage.show();
    }
    
    /**
     * JavaFX Host Services の取得
     * @return 
     */
    public HostServices getJavaFXHostServices() {
        return this.hostServices;
    }
}